package com.wuxf.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageInfo;
import com.wuxf.entity.DicDataInfo;
import com.wuxf.entity.InvoiceInfo;
import com.wuxf.entity.ResponseBodyEntity;
import com.wuxf.entity.UserInfo;
import com.wuxf.entity.custom.InvoiceCustom;
import com.wuxf.entity.custom.UserInfoCustom;
import com.wuxf.service.DicDataService;
import com.wuxf.service.InvoiceService;
import com.wuxf.service.TicketService;
import com.wuxf.service.UserService;

@Controller
@SuppressWarnings({"rawtypes","unchecked"})
public class FinanceController {
	@Autowired
	private DicDataService dicDataService;
	@Autowired
	private InvoiceService invoiceService;
	@Autowired
	private UserService userService;
	@Autowired
	private TicketService ticketService;
	/**
	 * 跳转到财务录入界面
	 * @param model
	 * @return
	 */
	@RequestMapping("finance/goToAddNumber")
	public String goToAddNumber(Model model,HttpSession session) {
		List<DicDataInfo> typeDicDatas = dicDataService.findByEnumTypeId("1");
		List<DicDataInfo> areaDicDatas = dicDataService.findByEnumTypeId("2");
		model.addAttribute("typeDicDatas", JSONArray.toJSONString(typeDicDatas));
		model.addAttribute("areaDicDatas", JSONArray.toJSONString(areaDicDatas));
		model.addAttribute("areaDicDatasArr", areaDicDatas);
		
		UserInfoCustom userInfoCustom = null;
		if(session.getAttribute("userInfoCustom") != null) {
			userInfoCustom = (UserInfoCustom)session.getAttribute("userInfoCustom");
			if(userInfoCustom.getUserAccount().equals("cw1")) {
				List<DicDataInfo> temp = new ArrayList<DicDataInfo>();
				for (int i = 0; i < typeDicDatas.size(); i++) {
					DicDataInfo dicDataInfo = typeDicDatas.get(i);
					if(dicDataInfo.getDicDataValue().equals("11")||
							dicDataInfo.getDicDataValue().equals("14")) {
						temp.add(dicDataInfo);
					}
				}
				model.addAttribute("typeDicDatasArr", temp);
			}else if(userInfoCustom.getUserAccount().equals("cw2")) {
				List<DicDataInfo> temp = new ArrayList<DicDataInfo>();
				for (int i = 0; i < typeDicDatas.size(); i++) {
					DicDataInfo dicDataInfo = typeDicDatas.get(i);
					if(dicDataInfo.getDicDataValue().equals("12")||
							dicDataInfo.getDicDataValue().equals("13")) {
						temp.add(dicDataInfo);
					}
				}
				model.addAttribute("typeDicDatasArr", temp);
			}else {
				model.addAttribute("typeDicDatasArr", typeDicDatas);
			}
		}
		return "finance/addNumber";
	}
	/**
	 * 录入发票
	 * @param session
	 * @param startNum
	 * @param endNum
	 * @param invoiceType
	 * @param areaId
	 * @return
	 */
	@RequestMapping("finance/addNum")
	public @ResponseBody Map addNum(HttpSession session, int invoiceNum,String invoiceType,int areaId) {
		Map resultMap = new HashMap();
		UserInfoCustom userInfoCustom = (UserInfoCustom)session.getAttribute("userInfoCustom");
		InvoiceInfo invoiceInfo = new InvoiceInfo();
		invoiceInfo.setAreaId(areaId);
		invoiceInfo.setInvoiceNum(invoiceNum);
		invoiceInfo.setInvoiceType(invoiceType);
		invoiceInfo.setUpdateUser(userInfoCustom.getUserId());
		invoiceInfo.setCreateUser(userInfoCustom.getUserId());
		invoiceService.saveInvoice(invoiceInfo);
		resultMap.put("state","1");
		return resultMap;
	}
	
	/**
	 * 跳转到财务查询
	 * @param model
	 * @return
	 */
	@RequestMapping("finance/goToQuery")
	public String goToQuery(Model model) {

		List<DicDataInfo> typeDicDatas = dicDataService.findByEnumTypeId("1");
		List<DicDataInfo> areaDicDatas = dicDataService.findByEnumTypeId("2");
		
		model.addAttribute("typeDicDatas", JSONArray.toJSONString(typeDicDatas));
		model.addAttribute("areaDicDatas", JSONArray.toJSONString(areaDicDatas));
		model.addAttribute("areaDicDatasArr", areaDicDatas);
		model.addAttribute("typeDicDatasArr", typeDicDatas);
		return "finance/query";
	}
	
	@RequestMapping("finance/goToQueryCount")
	public String goToQueryCount(Model model) {

		List<DicDataInfo> typeDicDatas = dicDataService.findByEnumTypeId("1");
		List<DicDataInfo> areaDicDatas = dicDataService.findByEnumTypeId("2");
		
		model.addAttribute("typeDicDatas", JSONArray.toJSONString(typeDicDatas));
		model.addAttribute("areaDicDatas", JSONArray.toJSONString(areaDicDatas));
		model.addAttribute("areaDicDatasArr", areaDicDatas);
		model.addAttribute("typeDicDatasArr", typeDicDatas);
		return "finance/queryCount";
	}
	
	@RequestMapping("finance/goToQueryByArea")
	public String goToQueryByArea(Model model) {

		List<DicDataInfo> typeDicDatas = dicDataService.findByEnumTypeId("1");
		List<DicDataInfo> areaDicDatas = dicDataService.findByEnumTypeId("2");
		
		model.addAttribute("typeDicDatas", JSONArray.toJSONString(typeDicDatas));
		model.addAttribute("areaDicDatas", JSONArray.toJSONString(areaDicDatas));
		model.addAttribute("areaDicDatasArr", areaDicDatas);
		model.addAttribute("typeDicDatasArr", typeDicDatas);
		return "finance/queryByArea";
	}
	
	@RequestMapping("finance/goToQueryByType")
	public String goToQueryByType(Model model) {

		List<DicDataInfo> typeDicDatas = dicDataService.findByEnumTypeId("1");
		List<DicDataInfo> areaDicDatas = dicDataService.findByEnumTypeId("2");
		
		model.addAttribute("typeDicDatas", JSONArray.toJSONString(typeDicDatas));
		model.addAttribute("areaDicDatas", JSONArray.toJSONString(areaDicDatas));
		model.addAttribute("areaDicDatasArr", areaDicDatas);
		model.addAttribute("typeDicDatasArr", typeDicDatas);
		return "finance/queryByType";
	}
	
	@RequestMapping("finance/goToEdit")
	public String goToEdit(Model model) {
		List<DicDataInfo> typeDicDatas = dicDataService.findByEnumTypeId("1");
		List<DicDataInfo> areaDicDatas = dicDataService.findByEnumTypeId("2");
		model.addAttribute("typeDicDatas", JSONArray.toJSONString(typeDicDatas));
		model.addAttribute("areaDicDatas", JSONArray.toJSONString(areaDicDatas));
		model.addAttribute("areaDicDatasArr", areaDicDatas);
		model.addAttribute("typeDicDatasArr", typeDicDatas);
		return "finance/edit";
	}
	
	@RequestMapping("finance/goToUpdateNum")
	public String goToUpdateNum(Model model,String invoiceId,String areaId,String ticketType) {
		model.addAttribute("areaName", dicDataService.findByEnumTypeId("2", areaId).get(0).getDicDataName());
		model.addAttribute("typeName", dicDataService.findByEnumTypeId("1", ticketType).get(0).getDicDataName());
		UserInfo userInfo = userService.findByAreaId(Integer.parseInt(areaId));
		InvoiceCustom invoiceCustom = invoiceService.findById(invoiceId);
		model.addAttribute("invoiceNum", invoiceCustom.getInvoiceNum());
		model.addAttribute("invoiceId", invoiceCustom.getInvoiceId());
		model.addAttribute("areaId", invoiceCustom.getAreaId());
		model.addAttribute("invoiceType", invoiceCustom.getInvoiceType());
		return "finance/updateNum";
	}
	
	@RequestMapping("finance/updateNum")
	public @ResponseBody ResponseBodyEntity updateNum(InvoiceInfo invoiceInfo) {
		ResponseBodyEntity responseBodyEntity = new ResponseBodyEntity();
		invoiceService.updateInvoice(invoiceInfo);
		responseBodyEntity.setState("1");
		return responseBodyEntity;
	}
	
	/**
	 * 财务查询
	 * @param model
	 * @return
	 */
	@RequestMapping("finance/findByMap")
	public @ResponseBody ResponseBodyEntity findByMap(HttpSession session,int page,int limit,InvoiceInfo invoiceInfo,String startDate,String endDate) {
		ResponseBodyEntity responseBodyEntity = new ResponseBodyEntity();
		PageInfo pageInfo = new PageInfo();
		pageInfo.setPageNum(page);
		pageInfo.setPageSize(limit);
		HashMap paraMap = new HashMap();
		
		UserInfoCustom userInfoCustom = null;
		if(session.getAttribute("userInfoCustom") != null) {
			userInfoCustom = (UserInfoCustom)session.getAttribute("userInfoCustom");
			if(!userInfoCustom.getRoleId().equals("0")) {
				paraMap.put("createUser", userInfoCustom.getUserId());
			}
		}
		paraMap.put("areaId", invoiceInfo.getAreaId());
		paraMap.put("invoiceType", invoiceInfo.getInvoiceType());
		paraMap.put("startDate", startDate);
		if(StringUtils.isNotBlank(endDate)) {
  			endDate = endDate+"3";
  		}
		paraMap.put("endDate", endDate);
		PageInfo<InvoiceCustom> invoiceInfos = invoiceService.findByMap(pageInfo,paraMap);
		responseBodyEntity.setCode(0);
		responseBodyEntity.setData(invoiceInfos.getList());
		responseBodyEntity.setCount(invoiceInfos.getTotal());
		return responseBodyEntity;
	}
	
	@RequestMapping("finance/findByMapAndCount")
	public @ResponseBody ResponseBodyEntity findByMapAndCount(HttpSession session,int page,int limit,InvoiceInfo invoiceInfo,String startDate,String endDate) {
		ResponseBodyEntity responseBodyEntity = new ResponseBodyEntity();
		PageInfo pageInfo = new PageInfo();
		pageInfo.setPageNum(page);
		pageInfo.setPageSize(limit);
		HashMap paraMap = new HashMap();
		
		UserInfoCustom userInfoCustom = null;
		if(session.getAttribute("userInfoCustom") != null) {
			userInfoCustom = (UserInfoCustom)session.getAttribute("userInfoCustom");
			if(!userInfoCustom.getRoleId().equals("0")) {
				paraMap.put("createUser", userInfoCustom.getUserId());
			}
		}
		paraMap.put("areaId", invoiceInfo.getAreaId());
		paraMap.put("invoiceType", invoiceInfo.getInvoiceType());
		paraMap.put("startDate", startDate);
		if(StringUtils.isNotBlank(endDate)) {
  			endDate = endDate+"3";
  		}
		paraMap.put("endDate", endDate);
		PageInfo<InvoiceCustom> invoiceInfos = invoiceService.findByMapAndCount(pageInfo,paraMap);
		responseBodyEntity.setCode(0);
		responseBodyEntity.setData(invoiceInfos.getList());
		responseBodyEntity.setCount(invoiceInfos.getTotal());
		return responseBodyEntity;
	}
	
	
	
	
	@RequestMapping("finance/findByMapGroupByType")
	public @ResponseBody ResponseBodyEntity findByMapGroupByType(HttpSession session,int page,int limit,InvoiceInfo invoiceInfo,String startDate,String endDate) {
		ResponseBodyEntity responseBodyEntity = new ResponseBodyEntity();
		PageInfo pageInfo = new PageInfo();
		pageInfo.setPageNum(page);
		pageInfo.setPageSize(limit);
		HashMap paraMap = new HashMap();
		
		UserInfoCustom userInfoCustom = null;
		if(session.getAttribute("userInfoCustom") != null) {
			userInfoCustom = (UserInfoCustom)session.getAttribute("userInfoCustom");
			if(!userInfoCustom.getRoleId().equals("0")) {
				paraMap.put("createUser", userInfoCustom.getUserId());
			}
		}
		paraMap.put("invoiceType", invoiceInfo.getInvoiceType());
		paraMap.put("startDate", startDate);
		if(StringUtils.isNotBlank(endDate)) {
  			endDate = endDate+"3";
  		}
		paraMap.put("endDate", endDate);
		PageInfo<InvoiceCustom> invoiceInfos = invoiceService.findByMapGroupByType(pageInfo,paraMap);
		responseBodyEntity.setCode(0);
		responseBodyEntity.setData(invoiceInfos.getList());
		responseBodyEntity.setCount(invoiceInfos.getTotal());
		return responseBodyEntity;
	}
	
	
	@RequestMapping("finance/findByMapGroupByAreaId")
	public @ResponseBody ResponseBodyEntity findByMapGroupByAreaId(HttpSession session,int page,int limit,InvoiceInfo invoiceInfo,String startDate,String endDate) {
		ResponseBodyEntity responseBodyEntity = new ResponseBodyEntity();
		PageInfo pageInfo = new PageInfo();
		pageInfo.setPageNum(page);
		pageInfo.setPageSize(limit);
		HashMap paraMap = new HashMap();
		
		UserInfoCustom userInfoCustom = null;
		if(session.getAttribute("userInfoCustom") != null) {
			userInfoCustom = (UserInfoCustom)session.getAttribute("userInfoCustom");
			if(!userInfoCustom.getRoleId().equals("0")) {
				paraMap.put("createUser", userInfoCustom.getUserId());
			}
		}
		paraMap.put("areaId", invoiceInfo.getAreaId());
		paraMap.put("startDate", startDate);
		if(StringUtils.isNotBlank(endDate)) {
  			endDate = endDate+"3";
  		}
		paraMap.put("endDate", endDate);
		PageInfo<InvoiceCustom> invoiceInfos = invoiceService.findByMapGroupByArea(pageInfo,paraMap);
		responseBodyEntity.setCode(0);
		responseBodyEntity.setData(invoiceInfos.getList());
		responseBodyEntity.setCount(invoiceInfos.getTotal());
		return responseBodyEntity;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	/**
	 * 领导查询详细记录
	 * @param model
	 * @return
	 */
	@RequestMapping("goToQueryDetails")
	public String goToQueryDetails(Model model) {
		String maxNum = "";//invoiceService.findMaxNum();
		model.addAttribute("maxNum", maxNum);
		return "updateNumber";
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	/**
	 * 更新发票
	 * @param session
	 * @param startNum
	 * @param endNum
	 * @param invoiceType
	 * @param areaId
	 * @return
	 
	@RequestMapping("finance/updateNum")
	public @ResponseBody Map updateNum(HttpSession session, String startNum,String endNum,String invoiceType,int areaId) {
		Map resultMap = new HashMap();
		UserInfoCustom userInfoCustom = (UserInfoCustom)session.getAttribute("userInfoCustom");
		InvoiceInfo invoiceInfo = new InvoiceInfo();
		invoiceInfo.setAreaId(areaId);
		invoiceInfo.setInvoiceType(invoiceType);
		invoiceInfo.setUpdateTime(DateUtils.getNowDate());
		invoiceInfo.setUpdateUser(userInfoCustom.getUserId());
		invoiceService.updateInvoice(startNum, endNum, invoiceInfo);
		resultMap.put("state","1");
		return resultMap;
	}*/
	
}
